﻿using BYSJ.Model;
using BYSJ.Web.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using BYSJ.IBLL;
using BYSJ.BLL;
using BYSJ.Web.Models.shipin;
using BYSJ.Web.Models.File;

namespace BYSJ.Web.Commmend
{
    public class Commend
    {
        /// <summary>
        /// 回答别表数据传递
        /// </summary>
        /// <param name="dbanswer"></param>
        /// <param name="answer"></param>
        /// <returns></returns>
        public static AnswerViewModel ListAQConvert(List<QuestingSet> dbanswer, AnswerSet answer)
        {
            AnswerViewModel answerviewmode = new AnswerViewModel();
            List<QuestingViewModel> questinflistmodel = new List<QuestingViewModel>();
            FirstTopViewModel firstmodel = new FirstTopViewModel();
            //读取一楼数据
            firstmodel.answerid = answer.id;
            firstmodel.answertime = answer.creattime;
            firstmodel.content = answer.content;
            firstmodel.firstimage = answer.UserSet.ImagePsth;
            firstmodel.firstname = answer.UserSet.Name;
            firstmodel.title = answer.title;
            answerviewmode.firsttopviewmodel = firstmodel;
            //读取其他数据
            foreach (var item in dbanswer)
            {
                QuestingViewModel questingviewmodel=new QuestingViewModel();
                questingviewmodel.content = item.content;
                questingviewmodel.firstimage = item.UserSet.ImagePsth;
                questingviewmodel.firstname = item.UserSet.Name;
                questingviewmodel.questingid = item.id;
                questingviewmodel.questingtime = item.creattime;
                List<HuiFuViewModel> huifuvoewlist = new List<HuiFuViewModel>();
                foreach (var huifuitem in item.HuiFuSet)
                {
                    HuiFuViewModel huifuvoew = new HuiFuViewModel();
                    huifuvoew.huifuzhe = huifuitem.UserSet.Name;
                    huifuvoew.huifushijian = huifuitem.creattime;
                    huifuvoew.imagepath = huifuitem.UserSet.ImagePsth;
                    huifuvoew.beihuifuzhe = huifuitem.content.Substring(0, huifuitem.content.IndexOf(":"));
                    huifuvoew.content = huifuitem.content.Substring(huifuitem.content.IndexOf(":")+1);
                    huifuvoewlist.Add(huifuvoew);
                }
                questingviewmodel.huifuview = huifuvoewlist;
                questinflistmodel.Add(questingviewmodel);
            }
            answerviewmode.firsttopviewmodel = firstmodel;
            answerviewmode.questinglist = questinflistmodel;
            return answerviewmode;
        }

        /// <summary>
        /// 提问别表数据传递
        /// </summary>
        /// <param name="dbanswer"></param>
        /// <returns></returns>
        public static List<huDong> ListConvert(List<AnswerSet> dbanswer)
        {
            List<huDong> BieBiaoView = new List<huDong>();
            foreach (var item in dbanswer)
            {
                huDong hudong = new huDong();
               
                //导航属性找到最新回复时间
                //var questing =.OrderByDescending(u => u.uptime).First();
                if ( item.QuestingSet.Count == 0)
                {
                    hudong.count = 0;
                    hudong.answertime = DateTime.Now.ToShortTimeString().ToString();
                    hudong.answername = "----------";
                }
                else
                {
                    var questing = item.QuestingSet.OrderByDescending(u => u.uptime).First();
                    hudong.count = item.QuestingSet.Count();
                    hudong.answertime = questing.uptime.ToShortTimeString().ToString();
                   
                    //导航属性找到最新回复用户
                    hudong.answername = questing.UserSet.Name;
                }
                hudong.name = item.UserSet.Name;
                hudong.creattime = string.Format("{0:M}",item.creattime);//11月5日 item.creattime.ToShortTimeString().ToString();
                //问题id
                hudong.Id = item.id;
                //用户id
                hudong.userid = item.userid;
                hudong.title = item.title;
                BieBiaoView.Add(hudong);
            }
            return BieBiaoView;
        }

        /// <summary>
        /// 视频首页数据转换
        /// </summary>
        /// <param name="video"></param>
        /// <returns></returns>
        public shiPin ShiPinConvert(List<VideoSet> video)
        {
            shiPin shipin = new shiPin();
            List<videl> listvideo=new List<videl>();
            List<biaoqianiten> biaoqian = new List<biaoqianiten>();
            foreach (var item in video)
            {
                videl videol = new videl();
                videol.Id = item.id;
                videol.Url = item.imageurl;
                videol.name = item.name;
                videol.count = item.VideoShowSet.Count;
                listvideo.Add(videol);
                biaoqianiten it = new biaoqianiten();
                it.name = item.fenlei;
                it.id = item.id.ToString();
                biaoqian.Add(it);
            }
            shipin.videl = listvideo;
            //去掉重复的序列 有问题
            shipin.bianqian = biaoqian.Distinct().ToList();
            return shipin;
        }

        /// <summary>
        /// 视频播放
        /// </summary>
        /// <param name="video"></param>
        /// <param name="videojilu">记录表数据</param>
        /// <returns></returns>
        public shipinshow ShiPinShowConvert(List<VideoShowSet> video, List<videojiluSet> videojilu, int ID)
        {
            shipinshow shipin = new shipinshow();
            shipin.nametop = video[0].VideoSet.name;
            shipin.topimageurl = video[0].VideoSet.imageurl;
            shipin.kechengcount = video.Count.ToString();
            List<bofanglei> listbofangle = new List<bofanglei>();
            double sum=0;
            //计算视频大小
            foreach (var item in video)
	        {
                bofanglei bofangle = new bofanglei();
		        sum+=(item.videosize/1024/1024);
                //获得视频url
                bofangle.bofangurl = item.videoshowurl;
                bofangle.biebiaoname = item.name.Substring(0, 14);
                bofangle.buttumname = item.name;
                bofangle.fengminurl = item.imageurl;
                //视频id
                bofangle.shipinid = item.id;
                //用户id
                bofangle.shipinuser = ID;
                int sub = item.videosize;
                string subwenzi=(sub/1024/1024 /2.95).ToString();
                if (subwenzi.IndexOf(".") <= -1)
                {
                    bofangle.shipintime = subwenzi + "min";
                }
                else
                {
                    int index = subwenzi.IndexOf(".");
                    bofangle.shipintime = subwenzi.Substring(0, index) + "min";
                }
               
                //循环遍历关联记录 查看用户id和视频id在videojilu中存不存在
                foreach (var itemjilu in videojilu)
                {
                    if (itemjilu.videoshouid==item.id&&itemjilu.userid==ID)
                    {
                        bofangle.biaoshifu = "1";
                    }
                    else
                    {
                        bofangle.biaoshifu = "0";
                    }
                }
                listbofangle.Add(bofangle);

        	}
            sum=sum/2.95/60;
            double mins=-Math.Floor((sum-Math.Floor(sum))*60);
            double hour=Math.Floor(sum);
            shipin.kechengtime = hour.ToString()+"小时 "+mins.ToString()+"分";
            shipin.yixuecount = videojilu.Count.ToString();
            double sum1=0;
            foreach (var item in videojilu)
            {
                sum1 += item.VideoShowSet.videosize;
            }
            sum1=Math.Floor(sum1/2.95/60);
            shipin.yixuetime = sum1.ToString();
            shipin.bofangle = listbofangle;
            return shipin;
        }

        /// <summary>
        /// 教学资源共享页面
        /// </summary>
        /// <returns></returns>
        public FileDown FileConvert(List<FileMangSet> filedb1, List<FileMangSet> filedb2, List<FileMangSet> filedb3, List<FileMangSet> filedb4)
        {
            FileDown filedoen = new FileDown();
            List<TongZhi> tongzhi = new List<TongZhi>();
            List<KeJian> kejian=new List<KeJian>();
            List<ZhiDao> zhidao=new List<ZhiDao>();
            List<Other> other = new List<Other>(); 
            foreach (var item in filedb1)
	        {
                TongZhi tz = new TongZhi();
                tz.FilecreatTime = item.FilecreatTime.ToShortDateString().ToString();
                tz.Fileleibie = item.Fileleibie;
                tz.Fileleibiename = item.Fileleibiename;
                tz.FileName = item.FileName;
                tz.FileUrl = item.FileUrl;
                tz.Id = item.id;
                tz.username = item.UserSet.Name;
                tongzhi.Add(tz);
            }
             foreach (var item in filedb2)
	        {
		        KeJian ke=new KeJian();
                ke.FilecreatTime = item.FilecreatTime.ToShortDateString().ToString();
                ke.Fileleibie = item.Fileleibie;
                ke.Fileleibiename = item.Fileleibiename;
                ke.FileName = item.FileName;
                ke.FileUrl = item.FileUrl;
                ke.Id = item.id;
                ke.username = item.UserSet.Name;
                kejian.Add(ke);
	        }
            foreach (var item in filedb3)
	        {
		        ZhiDao zd=new ZhiDao();
                zd.FilecreatTime = item.FilecreatTime.ToShortDateString().ToString();
                zd.Fileleibie = item.Fileleibie;
                zd.Fileleibiename = item.Fileleibiename;
                zd.FileName = item.FileName;
                zd.FileUrl = item.FileUrl;
                zd.Id = item.id;
                zd.username = item.UserSet.Name;
                zhidao.Add(zd);
	        }
            foreach (var item in filedb4)
	        {
                Other ot = new Other();
                ot.FilecreatTime = item.FilecreatTime.ToShortDateString().ToString();
                ot.Fileleibie = item.Fileleibie;
                ot.Fileleibiename = item.Fileleibiename;
                ot.FileName = item.FileName;
                ot.FileUrl = item.FileUrl;
                ot.username = item.UserSet.Name;
                ot.Id = item.id;
                other.Add(ot);
	        }
            filedoen.kejian = kejian;
            filedoen.tongzhi = tongzhi;
            filedoen.zhidao = zhidao;
            filedoen.other = other;
            return filedoen;
        }

        /// <summary>
        /// 文件分类view转换
        /// </summary>
        /// <param name="dbfile"></param>
        /// <returns></returns>
        public FenLeiFile FilesConvert(List<FileMangSet> dbfile)
        {
            FenLeiFile fenleiFiles = new FenLeiFile();
            List<Files> listFile = new List<Files>();
            foreach (var item in dbfile)
            {
                Files files = new Files();
                files.FilecreatTime = item.FilecreatTime.ToShortDateString().ToString();
                files.Fileleibie = item.Fileleibie;
                files.Fileleibiename = item.Fileleibiename;
                files.FileName = item.FileName;
                files.FileUrl = item.FileUrl;
                files.Id = item.id;
                files.username = item.UserSet.Name;
                listFile.Add(files);
            }
            fenleiFiles.Files = listFile;
            return fenleiFiles;
        }
        /// <summary>
        /// 资源共享view转换
        /// </summary>
        /// <param name="dbfile"></param>
        /// <returns></returns>
        public Share shareConvert(List<FileMangSet> dbfile, int pageindex, int totalCount, List<FileMangSet> dbfileTh)
        {
            Share fenleiFiles = new Share();
            List<Files> listFile = new List<Files>();
            List<ShareTh> shareListTH = new List<ShareTh>();
            foreach (var item in dbfile)
            {
                Files files = new Files();
                files.FilecreatTime = item.FilecreatTime.ToShortDateString().ToString();
                files.Fileleibie = item.Fileleibie;
                files.Fileleibiename = item.Fileleibiename;
                files.FileName = item.FileName;
                files.FileUrl = item.FileUrl;
                files.Id = item.id;
                files.username = item.UserSet.Name;
                listFile.Add(files);
            }
            foreach (var item in dbfileTh)
            {
                ShareTh sherth = new ShareTh();
                sherth.name = item.FileName;
                sherth.url = item.FileUrl;
                shareListTH.Add(sherth);
            }
            fenleiFiles.Files = listFile;
            fenleiFiles.fileTh = shareListTH;
            if (totalCount > 0)
            {
                //一共多少行数据
                fenleiFiles.contt = totalCount;
                //第几页数据
                fenleiFiles.page = pageindex;
                //一共多少页数据
                float num = 11;
                float pages = totalCount / num;
                fenleiFiles.pages = (int)Math.Ceiling(pages);
            }
            else
            {
                fenleiFiles.contt = 1;
                //第几页数据
                fenleiFiles.page = 1;
                //一共多少页数据
                fenleiFiles.pages =1;
            }
            return fenleiFiles;
        }

        /// <summary>
        /// 首页侧边栏数据转化
        /// </summary>
        /// <returns></returns>
        public UserView indexCebianlan(List<videojiluSet> shipinCebian, List<FileMangSet> dbfilesTh, List<VideoSet> videoTuiJian, List<FileMangSet> dbfilesThziliao, List<AnswerSet> dbanswer, List<videojiluSet> jiluresulr, List<KaiYuanShiPin> KaiYuanVideo)
        {
            UserView us=new UserView();
               //存剩余的数据
            List<videojiluSet> video = new List<videojiluSet>();
            List<shipincebian> shipin=new List<shipincebian>();
            List<filecebian> file = new List<filecebian>();
            List<TuiJian> VideoTuiJian = new List<TuiJian>();
            List<indexStart> indexstart = new List<indexStart>();
            List<daHuiShiPin> kaiyuan = new List<daHuiShiPin>();
            indexHudong[] indexhudong = new indexHudong[6];
            //去掉重复数据
            if (shipinCebian != null && shipinCebian.Count > 0)
            {
                for (int i = 0; i < shipinCebian.Count; i++)
                {
                    for (int j =  shipinCebian.Count-1; j >i; j--)
                    {
                        if (shipinCebian[i].videoshouid == shipinCebian[j].videoshouid)
                        {
                            shipinCebian.RemoveAt(j);
                        }
                    }
                }
            }
            foreach (var item in shipinCebian)
            {
                if (video.Count >= 8)
                    break;
                video.Add(item);
            }
            foreach (var item in video)
            {
                shipincebian vi = new shipincebian();
                //视频video表的id
                vi.id = item.VideoShowSet.VideoSet.id.ToString();
                vi.fenleiId = item.VideoShowSet.VideoSet.fenlei;
                vi.videoUserName = item.UserSet.Name;
                vi.name = item.VideoShowSet.videoname;
                shipin.Add(vi);
            }
            foreach (var item in dbfilesTh)
            {
                filecebian fi = new filecebian();
                fi.id = item.id.ToString();
                fi.fenleiId = item.Fileleibie.ToString();
                fi.name = item.FileName;
                fi.fileusername = item.UserSet.Name;
                file.Add(fi);
            }
            foreach (var item in videoTuiJian)
            {
                TuiJian tujian = new TuiJian();
                tujian.id = item.id.ToString();
                tujian.biaoqianName = item.fenlei;
                tujian.name = item.name;
                tujian.videoIamge = item.VideoUrl;
                VideoTuiJian.Add(tujian);
            }
            ziliaoku ziliao = new ziliaoku();
            ziliao.count = dbfilesThziliao.Count();
            //将筛选的数据存里面
            List<string> ziliaostring = new List<string>();

            if (dbfilesThziliao != null && dbfilesThziliao.Count > 0)
            {
                for (int i = 0; i < dbfilesThziliao.Count; i++)
                {
                    for (int j = dbfilesThziliao.Count - 1; j >i; j--)
                    {
                        if (dbfilesThziliao[i].Fileleibiename == dbfilesThziliao[j].Fileleibiename)
                        {
                            dbfilesThziliao.RemoveAt(j);
                        }
                    }
                }
            }
            foreach (var item in dbfilesThziliao)
            {
                ziliaostring.Add(item.Fileleibiename);
            }
            for (int i = 0; i < 6; i++)
			{
                if (i < dbanswer.Count())
                {
                    indexHudong an = new indexHudong();
                    an.id = dbanswer[i].id.ToString();
                    an.title = dbanswer[i].title;
                    an.content = dbanswer[i].content;
                    an.inageUrl = dbanswer[i].UserSet.ImagePsth;
                    an.user = dbanswer[i].UserSet.Name;
                    indexhudong[i] = an;
                }
			}
            for (int i = 0; i <=5; i++)
            {
                if (indexhudong[i]==null)
                {
                      indexHudong an = new indexHudong();
                      indexhudong[i] = an;
                }
            }
            foreach (var item in jiluresulr)
            {
                indexStart st = new indexStart();
                st.name = item.UserSet.Name;
                st.time = item.crettime.Month.ToString() + "-" + item.crettime.Day.ToString() + " " + item.crettime.Hour.ToString() + ":" + item.crettime.Second.ToString();
                st.title = item.VideoShowSet.videoname;
                indexstart.Add(st);
            }
            foreach (var item in KaiYuanVideo)
            {
                daHuiShiPin hs = new daHuiShiPin();
                hs.id = item.id.ToString();
                hs.imageUrl = item.imageUrl;
                hs.name = item.name;
                hs.videoUrl = item.videoUrl;
                kaiyuan.Add(hs);
            }
            ziliao.fenleinane = ziliaostring;
            us.ziliao = ziliao;
            us.xiaobianTuijian = VideoTuiJian;
            us.shipin = shipin;
            us.file = file;
            us.indexhudong = indexhudong;
            us.indexstart = indexstart;
            us.indexKaiYuan = kaiyuan;
            return us;
        }
    }
}